package io.renren.modules.substation.service.impl;

import io.renren.common.utils.DateUtils;
import io.renren.modules.substation.entity.TInfoAreaInfoEntity;
import io.renren.modules.substation.entity.TInfoDevInfoEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.substation.dao.TBusiSysAlarmInfoDao;
import io.renren.modules.substation.entity.TBusiSysAlarmInfoEntity;
import io.renren.modules.substation.service.TBusiSysAlarmInfoService;


@Service("tBusiSysAlarmInfoService")
public class TBusiSysAlarmInfoServiceImpl extends ServiceImpl<TBusiSysAlarmInfoDao, TBusiSysAlarmInfoEntity> implements TBusiSysAlarmInfoService {

    @Autowired
    private TInfoDevInfoServiceImpl tInfoDevInfoServiceImpl;
    @Autowired
    private TInfoAreaInfoServiceImpl tInfoAreaInfoServiceImpl;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String alarmTimeStart = null;
        if (params.get("alarmTimeStart") != null) {
            String oldDate = params.get("alarmTimeStart").toString().replace("Z", " UTC");
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
            try {
                Date date = df.parse(oldDate);
                alarmTimeStart = DateUtils.format(date, "yyyyMMddHHmmss");
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        String alarmTimeEnd = null;
        if (params.get("alarmTimeEnd") != null) {
            String oldDate = params.get("alarmTimeEnd").toString().replace("Z", " UTC");
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
            try {
                Date date = df.parse(oldDate);
                alarmTimeEnd = DateUtils.format(date, "yyyyMMddHHmmss");
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        String alarmSource = params.get("alarmSource") != null ? params.get("alarmSource").toString() : null;
        String alarmType = params.get("alarmType") != null ? params.get("alarmType").toString() : null;
        String readFlag = params.get("readFlag") != null ? params.get("readFlag").toString() : null;
        IPage<TBusiSysAlarmInfoEntity> page = null;
        boolean timeFlag = false;
        if (alarmTimeStart != null && alarmTimeEnd != null) {
            timeFlag = true;
        }

        String sortColumn = params.get("sort") != null ? params.get("sort").toString() : null;
        page = this.page(
                new Query<TBusiSysAlarmInfoEntity>().getPage(params),
                new QueryWrapper<TBusiSysAlarmInfoEntity>()
                        .eq(StringUtils.isNotBlank(alarmType),"ALARM_TYPE", alarmType)
                        .eq(StringUtils.isNotBlank(readFlag),"READ_FLAG", readFlag)
                        .eq(StringUtils.isNotBlank(alarmSource),"ALARM_SOURCE", alarmSource)
                        .between(timeFlag, "ALARM_TIME", alarmTimeStart, alarmTimeEnd)
                        .orderByDesc(sortColumn != null, sortColumn)
        );

        return new PageUtils(page);
    }

    @Override
    public int insertAlarmInfoBySystem(String time, String devCode, String variable, String desc) {
        Long devArea = null;
        List<TInfoDevInfoEntity> list = tInfoDevInfoServiceImpl.getDevInfoByDevCode(devCode);
        if (list != null && list.size() > 0){
            devArea = list.get(0).getDevArea();
        }
        String areaAddr = "";
        TInfoAreaInfoEntity area = tInfoAreaInfoServiceImpl.getById(devArea);
        if (area != null){
            areaAddr = area.getAreaAddr();
        }
        String type = getVariableDescName(variable);

        TBusiSysAlarmInfoEntity obj = new TBusiSysAlarmInfoEntity();
        obj.setAlarmTime(time);
        obj.setAlarmSource(devCode);
        obj.setAlarmArea(devArea);
        obj.setAreaAddr(areaAddr);
        obj.setAlarmType(type.equals("") ? variable : type);
        obj.setAlarmDesc(desc);
        obj.setAlarmLevel((long)0);
        obj.setCreateOpr((long)0);
        obj.setCreateTime(new Date());
        return baseMapper.insert(obj);
    }

    @Override
    public int getUnReadCount() {
        return baseMapper.selectCount(new QueryWrapper<TBusiSysAlarmInfoEntity>()
                .eq("READ_FLAG","0"));
    }

    private String getVariableDescName(String desc){
        String name = "";
        String kind = desc.substring(0, 2);
        if ("00".equals(kind))
            name += desc.substring(2, desc.length());
        else
            name += "环境量" + desc.substring(1, 2) + "-" + desc.substring(2, desc.length());
        return name;
    }

}